package cn.jingsheng.backup.util;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

import cn.jingsheng.backup.Constants;
import cn.jingsheng.backup.bean.UserLogin;

/**
 * 公共通用方法
 * 
 * @author 李晓丹
 * 
 */
public class CommonUtil {
	/**
	 * 校验用户是否已经登录
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isLogin(HttpSession session) {
		UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
		return userLogin != null;
	}

	/**
	 * 校验用户是否还未登录
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isNotLogin(HttpSession session) {
		return !isLogin(session);
	}

	/**
	 * 拥有管理员权限
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isAdministrator(HttpSession session) {
		// 未登录
		if (isNotLogin(session)) {
			return false;
		}
		UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
		// 删除
		boolean checkRole = (userLogin.getRoleId() & Constants.ROLE_DELETE) != 0;
		// 查看
		checkRole = checkRole && (userLogin.getRoleId() & Constants.ROLE_VIEW) != 0;
		// 下载
		checkRole = checkRole && (userLogin.getRoleId() & Constants.ROLE_DOWN) != 0;
		// 上传
		checkRole = checkRole && (userLogin.getRoleId() & Constants.ROLE_UPLOAD) != 0;
		return checkRole;
	}

	/**
	 * 拥有编辑权限
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isEditor(HttpSession session) {
		// 未登录
		if (isNotLogin(session)) {
			return false;
		}
		UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
		// 删除
		boolean checkRole = (userLogin.getRoleId() & Constants.ROLE_DELETE) != 0;
		// 查看
		checkRole = checkRole && (userLogin.getRoleId() & Constants.ROLE_VIEW) != 0;
		// 下载
		checkRole = checkRole && (userLogin.getRoleId() & Constants.ROLE_DOWN) != 0;
		// 上传
		checkRole = checkRole && (userLogin.getRoleId() & Constants.ROLE_UPLOAD) != 0;
		return checkRole;
	}

	/**
	 * 拥有查看权限
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isViewer(HttpSession session) {
		// 未登录
		if (isNotLogin(session)) {
			return false;
		}
		UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
		// 查看
		boolean checkRole = (userLogin.getRoleId() & Constants.ROLE_VIEW) != 0;
		// 删除
		// checkRole = (userLogin.getRoleId() & Constants.ROLE_DELETE) != 0;
		// 下载
		// checkRole = checkRole && (userLogin.getRoleId() &
		// Constants.ROLE_DOWN) != 0;
		// 上传
		// checkRole = checkRole && (userLogin.getRoleId() &
		// Constants.ROLE_UPLOAD) != 0;
		return checkRole;
	}

	public static HashMap<String, Integer> rolesAfterModify = new HashMap<String, Integer>();

	/**
	 * 检查并获取用户权限,用于批量修改权限后立即生效
	 * 
	 * @param username
	 * @return
	 */
	public static void referRoleId(UserLogin user) {
		if (user == null) {
			return;
		}
		Integer roleId = rolesAfterModify.get(user.getUserName());
		// 判断是否需要更新权限
		if (roleId == null) {
			return;
		}
		// 取出后立即移除
		user.setRoleId(roleId);
		rolesAfterModify.remove(user.getUserName());
	}
}
